class Solution 
{
public:

    int fun(int n)
    {
        int sum = 0;

        while (n)
        {
            int t = n % 10;
            sum += t * t;
            n /= 10;
        }

        return sum;
    }
    bool isHappy(int n)
    {
        int slow = n;
        int fast = fun(n);

        while (slow != fast)
        {
            slow = fun(slow);
            fast = fun(fun(fast));
        }
        return slow == 1;
    }
};